<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>药物采购管理</span>
      </div>
      <!--搜索-->
      <el-form
        :inline="true"
        :model="tableData"
        class="demo-form-inline"
        style="padding-right: -17px;padding: 0px 20px;"
      >
        <el-form-item label="药品名称" prop="drugName">
          <el-input
            v-model="tableData.drugName"
            placeholder="药品名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="tableData.dateValue"
            type="date"
            placeholder="选择过期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="药品规格">
          <el-select v-model="tableData.drugType" placeholder="药品规格">
            <el-option value="" label="请选择药品规格"></el-option>
            <el-option
              v-for="d in drugType"
              :key="d.specification"
              :value="d.id"
              :label="d.dosage"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('getDrug')"
            >查询</el-button
          >
        </el-form-item>
        <el-upload
          class="upload-demo"
          action="/api/biz/purchase/fileUpload"
          :on-preview="handlePreview"
          :on-success="uploadSuccess"
          :file-list="fileList"
        >
          <el-button type="primary">导入</el-button>
        </el-upload>
        <el-button @click="exportData" type="success">导出</el-button>
        <el-button @click="addDrug" type="success">增加药品</el-button>
      </el-form>

      <!--表格-->
      <el-table :data="drug">
        <el-table-column
          fixed
          prop="drugName"
          label="药品名称"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="drugTypePojo.specification"
          label="剂量"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="drugTypePojo.dosage"
          label="规格"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="drugCoding"
          label="药品本位码"
          width="140"
        ></el-table-column>
        <el-table-column
          prop="drugCreationDate"
          label="批次"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="drugExpirationDate"
          label="效期"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="drugAmount"
          label="数量"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="drugInventory"
          label="库位"
          width="120"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" width="240">
          <template slot-scope="scope">
            <el-button
              @click.native="updateDrug(scope.row.id)"
              type="text"
              size="small"
            >
              <i class="el-icon-plus">修改</i>
            </el-button>
            <el-button
              @click.native="deleteRow(scope.row.drugName, scope.row.id)"
              type="text"
              size="small"
            >
              <i class="el-icon-delete-solid">删除</i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="tableData.currentPage"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="tableData.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totals"
        >
        </el-pagination>
      </div>
    </el-card>

    <!--弹出修改页面框-->
    <el-dialog title="药品信息" :visible.sync="update" width="50%">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="药品名称">
          <el-input
            v-model="formInline.drugName"
            placeholder="药品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="药品编码">
          <el-input
            v-model="formInline.drugCoding"
            :disabled="true"
            placeholder="药品编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品数量">
          <el-input
            v-model="formInline.drugAmount"
            placeholder="数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="库存数量">
          <el-input
            v-model="formInline.drugInventory"
            placeholder="库存"
          ></el-input>
        </el-form-item>
        <el-form-item label="规格" style="padding-right: 265px;">
          <el-select v-model="formInline.drugType" placeholder="规格">
            <el-option
              v-for="d in drugType"
              :key="d.specification"
              :value="d.id"
              :label="d.dosage"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-input
          style="margin-bottom:15px;width: 80%;"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="药品功效"
          v-model="formInline.drugEffect"
        >
        </el-input>
        <br />
        <el-form-item>
          <el-button type="primary" @click="onSubmitUpdate">修改药品</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--弹出增加药品页面框-->
    <el-dialog title="药品信息" :visible.sync="addKapian" width="50%">
      <el-form
        :inline="true"
        :model="addDrugFrom"
        class="demo-form-inline"
        :rules="rules"
        ref="addDrugFrom"
      >
        <el-form-item label="药品名称" prop="drugName">
          <el-input
            v-model="addDrugFrom.drugName"
            placeholder="药品名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="药品编码" prop="drugCoding">
          <el-input
            v-model="addDrugFrom.drugCoding"
            placeholder="药品编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="产品数量" prop="drugAmount">
          <el-input
            v-model="addDrugFrom.drugAmount"
            placeholder="数量"
          ></el-input>
        </el-form-item>
        <el-form-item label="库存数量" prop="drugInventory">
          <el-input
            v-model="addDrugFrom.drugInventory"
            placeholder="库存"
          ></el-input>
        </el-form-item>
        <el-form-item prop="drugType" label="规格" style="padding-left: 20px;">
          <el-select
            v-model="addDrugFrom.drugType"
            placeholder="请选择药品规格"
          >
            <el-option
              v-for="d in drugType"
              :key="d.specification"
              :value="d.id"
              :label="d.dosage"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药品价格" prop="drugPrice">
          <el-input
            v-model="addDrugFrom.drugPrice"
            placeholder="价格"
          ></el-input>
        </el-form-item>
        <el-form-item label="生产日期" prop="drugCreationDate">
          <el-input
            v-model="addDrugFrom.drugCreationDate"
            placeholder="生产日期"
          ></el-input>
        </el-form-item>
        <el-form-item label="过期日期" prop="drugExpirationDate">
          <el-input
            v-model="addDrugFrom.drugExpirationDate"
            placeholder="过期日期"
          ></el-input>
        </el-form-item>
        <el-input
          style="margin: 20px 0px;width: 80%;"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="药品功效"
          v-model="addDrugFrom.drugEffect"
        >
        </el-input>
        <br />
        <el-form-item>
          <el-button type="primary" @click="onSubmitAdd">添加药品</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- xlsx数据列表 -->

    <el-dialog title="数据确认" :visible.sync="xlsxDataListVisible" width="90%">
      <el-table
        :data="importData"
        height="450"
        border
        style="width: 100%"
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-background="rgb(255,255,255)"
      >
        <el-table-column prop="drugName" label="药品名称"> </el-table-column>
        <el-table-column prop="drugCoding" label="药品编号"> </el-table-column>
        <el-table-column prop="drugCreationDate" label="药品生成日期">
        </el-table-column>
        <el-table-column prop="drugExpirationDate" label="药品过期日期">
        </el-table-column>
        <el-table-column prop="drugInventory" label="药品库存">
        </el-table-column>
        <el-table-column prop="drugType" label="药品类型"> </el-table-column>
        <el-table-column prop="drugPrice" label="药品价格"> </el-table-column>
        <el-table-column prop="drugAmount" label="药品数量"> </el-table-column>
        <el-table-column prop="drugEffect" label="药品功效"> </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button @click="xlsxDataListVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitXlsxData">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 过期表单数据 -->
    <el-dialog title="这是即将过期的药品" :visible.sync="expireDrugVisible">
      <!-- 表单数据 -->
      <el-table :data="drugExpire" style="width: 100%">
        <el-table-column prop="id" label="药品ID"> </el-table-column>
        <el-table-column prop="drugName" label="药品名称"> </el-table-column>
        <el-table-column prop="drugCreationDate" label="入库时间">
        </el-table-column>
        <el-table-column prop="drugExpirationDate" label="到期时间">
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-row type="flex" class="row-bg" justify="center">
          <el-checkbox v-model="checked">今天不再提示</el-checkbox>
        </el-row>
        <p></p>
        <el-row type="flex" class="row-bg" justify="center">
          <el-button type="primary" @click="determine">确 定</el-button>
        </el-row>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: {
        drugName: "",
        drugType: "",
        dateValue: "",
        currentPage: 1,
        pageSize: 5
      },
      drug: [],
      drugType: [],
      totals: 0,
      update: false,
      addKapian: false,
      formInline: [],
      addDrugFrom: {
        drugName: "",
        drugCoding: "",
        drugAmount: "",
        drugInventory: "",
        drugPrice: "",
        drugType: "",
        drugCreationDate: "",
        drugExpirationDate: "",
        drugEffect: ""
      },
      rules: {
        drugName: [
          { required: true, message: "请输入药品名称", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],
        drugCoding: [
          { required: true, message: "请输入药品编码", trigger: "blur" },
          { min: 6, max: 10, message: "长度在 6 到 10 个字符", trigger: "blur" }
        ],
        drugAmount: [
          { required: true, message: "请输入药品数量", trigger: "blur" }
        ],
        drugInventory: [
          { required: true, message: "请输入药品库存", trigger: "blur" }
        ],
        drugPrice: [
          { required: true, message: "请输入药品价格", trigger: "blur" }
        ],
        drugCreationDate: [
          { required: true, message: "请输入生成日期", trigger: "blur" }
        ],
        drugExpirationDate: [
          { required: true, message: "请输入过期日期", trigger: "blur" }
        ]
      },
      fileList: [],
      xlsxDataListVisible: false,
      importData: [],
      loading: false,
      expireDrugVisible: false,
      checked: false,
      // 表单数据
      drugExpire: []
    };
  },
  methods: {
    //查询药品信息
    onSubmit() {
      this.getDrug();
    },
    //点击查询药品信息方法
    getDrug() {
      //发送请求(药品信息)
      this.$axios.post("/api/biz/purchase/list", this.tableData).then(res => {
        let data = res.data;
        debugger;
        if (data.code == 0) {
          if (data.data.data.length == 0) {
            this.$message.success("没有数据");
          }
          this.drug = data.data.data;
          this.totals = data.data.totals;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //查询单个药品
    updateDrug(id) {
      this.update = true;
      //发送请求(药品信息)
      this.$axios.get("/api/biz/drug/selectID/" + id).then(res => {
        let data = res.data;
        if (data.code == 0) {
          this.formInline = data.data;
        } else {
          this.$message.error(data.error);
        }
      });
    },
    //修改药品信息
    onSubmitUpdate() {
      //发送请求(药品信息)
      this.$axios
        .post("/api/biz/purchase/modify", this.formInline)
        .then(res => {
          let data = res.data;
          if (data.code == 0) {
            this.$message.success(data.msg);
            this.update = false;
          } else {
            this.$message.success(data.msg);
          }
        });
    },
    //设置每页多少条
    handleSizeChange(pageSize) {
      this.tableData.pageSize = pageSize;
      this.getDrug();
    },
    //下一页
    handleCurrentChange(currentPage) {
      this.tableData.currentPage = currentPage;
      this.getDrug();
    },
    //跳转增加药品信息
    addDrug() {
      this.addKapian = true;
      //发送请求(药品信息)
      this.drugTypeList();
    },
    //增加药品信息
    onSubmitAdd() {
      //发送请求(药品规格)
      this.$axios.post("/api/biz/purchase/add", this.addDrugFrom).then(res => {
        let data = res.data;
        if (data.code == 0) {
          this.$message.success(data.msg);
          this.addKapian = false;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //删除药品
    deleteRow(drugName, id) {
      this.$confirm("此操作将永久删除" + drugName + ", 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$axios.delete("/api/biz/purchase/del/" + id).then(res => {
          debugger;
          let data = res.data;
          if (data.code == 0) {
            this.$message.success(data.msg);
            this.getDrug();
          } else {
            this.$message.success(data.msg);
          }
        });
      });
    },
    drugTypeList() {
      //发送请求(药品规格)
      this.$axios.post("/api/biz/drugType/list").then(res => {
        let data = res.data;
        if (data.code == 0) {
          this.drugType = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    /**
     * 文件选择
     */
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    /**
     * 文件上传成功
     */
    uploadSuccess() {
      this.xlsxDataListVisible = true;
      this.$axios
        .get("/api/biz/import/data")
        .then(result => {
          let data = result.data;
          if (data.code == 0) {
            this.importData = data.data;
            let text = document.getElementsByClassName(
              "el-upload-list--text"
            )[0];
            text.style.display = "none";
            this.getDrug();
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },
    /**
     * Xlsx提交按钮
     */
    submitXlsxData() {
      this.loading = true;
      this.$axios
        .get("/api/biz/submit/xlsx")
        .then(result => {
          let data = result.data;
          if (data.code == 0) {
            this.xlsxDataListVisible = false;
            this.loading = false;
            this.$message.success("新增成功！");
          } else {
            this.$message.error("数据新增失败！");
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },
    /**
     * 导出Xlsx数据
     */
    exportData() {
      this.$axios
        .get("/api/biz/download/file/drugList.xlsx")
        .then(result => {
          let data = result.data;
          if (data.code == 0) {
            //下载链接
            window.open(data.data);
          } else {
            this.$message.error(data.msg);
          }
        })
        .catch(err => {
          this.$message.error(err);
        });
    },
    /**
     * 确认关闭
     */
    determine() {
      if (this.checked) {
        this.$axios
          .get("/api/biz/drug/expire/clear")
          .then(result => {
            if (result.data.code == 0) {
              this.expireDrugVisible = false;
            }
          })
          .catch(err => {
            this.$message.error(err);
          });
      }else{
          this.expireDrugVisible = false;
      }
    }
  },
  //自动加载
  mounted() {
    //发送请求(药品信息)
    this.$axios.post("/api/biz/purchase/list", this.tableData).then(res => {
      let data = res.data;
      if (data.code == 0) {
        this.drug = data.data.data;
        this.totals = data.data.totals;
      } else {
        this.$message.error(data.msg);
      }
    });
    //发送请求(药品规格)
    this.drugTypeList();

    //获取过期药品数据
    this.$axios
      .get("/api/biz/drug/expire")
      .then(result => {
        console.log(result.data.data);
        if (result.data.data == null) {
          this.expireDrugVisible = false;
          this.drugExpire = [];
        } else {
          this.expireDrugVisible = true;
          this.drugExpire = result.data.data;
        }
      })
      .catch(err => {
        this.$message.error(err);
      });
  }
};
</script>

<style scoped>
.el-input--suffix .el-input__inner {
  padding-right: 20px;
}
.el-pagination {
  margin: 30px 0px;
}
.el-table th.el-table__cell > .cell {
  text-align: center;
}
.el-table--enable-row-transition .el-table__body td.el-table__cell {
  text-align: center;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: auto;
  margin-top: 0;
}
.upload-demo {
  display: inline-block;
}
</style>
